﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using GP.Scripts.Battle;
using Gp.Scripts.Data;
using Gp.Scripts.Equip;

namespace Gp.Scripts.Core {
    /// <summary>
    /// 职业
    /// </summary>
    public class UnitProCom : IUnitComponent {
        public BaseUnit Unit { get; private set; }
        // public BaseProfession Profession => Unit.prefabData.profession.Profession;

        // public UnitProfessionData ProfessionData => null;

        private readonly List<BaseSpecialty> _specialties = new();


        public void Init(BaseUnit unit) {
            Unit = unit;
            // OnUpdate();
            // _specialties.AddRange(ProfessionData.specialties.Where(x => x != null).Select(x => x.Build()));
            //
            // // 初始化专长
            // foreach (var s in Specialties()) {
            //     s.Init(unit);
            //     s.OnApply();
            // }
        }


        public void OnUpdate() { }


        public IEnumerator OnTurnStart() {
            return Specialties().Select(x => x.OnSmallTurnStart()).GetEnumerator();
        }
        
        public IEnumerator OnTurnEnd() {
            return Specialties().Select(x => x.OnSmallTurnEnd()).GetEnumerator();
        }


        public IEnumerable<BonusInfo> Bonus() {
            var res = new List<BonusInfo>();
            res.AddRange(Specialties().Select(specialty => new BonusInfo(specialty.Bonus(), specialty)));
            return res;
        }


        public IEnumerable<WeaponAbilityEntity> GetWeaponAbilities(WeaponEntity weapon) {
            return Specialties()
                .OfType<BaseWeaponSpecialty>()
                .Where(x => x.Code == weapon.TypeCode)
                .SelectMany(x => x.WeaponAbility(weapon));
        }

        
        
        // public bool ContainsSpecialty<T>() where T : BaseSpecialty {
        //     return _specialtyMap.ContainsKey(typeof(T));
        // }

        public T GetSpecialty<T>() where T : BaseSpecialty {
            return Specialties().FirstOrDefault(x => x is T) as T;
        }


        public IEnumerable<BaseSpecialty> Specialties() {
            return _specialties;
        }

        public void OnDestroy()
        {
            // this._specialties.Clear();
        }
    }
}